home *** CD-ROM | disk | FTP | other *** search
- package javax.crypto;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.security.AlgorithmParameters;
- import java.security.InvalidAlgorithmParameterException;
- import java.security.InvalidKeyException;
- import java.security.Key;
- import java.security.NoSuchAlgorithmException;
- import java.security.NoSuchProviderException;
-
- public class SealedObject implements Serializable {
- static final long serialVersionUID = 4482838265551344752L;
- private byte[] encryptedContent = null;
- private String sealAlg = null;
- private String paramsAlg = null;
- protected byte[] encodedParams = null;
-
- public SealedObject(Serializable var1, Cipher var2) throws IOException, IllegalBlockSizeException {
- ByteArrayOutputStream var3 = new ByteArrayOutputStream();
- ObjectOutputStream var4 = new ObjectOutputStream(var3);
-
- byte[] var5;
- try {
- var4.writeObject(var1);
- var4.flush();
- var5 = var3.toByteArray();
- } finally {
- var4.close();
- }
-
- try {
- this.encryptedContent = var2.doFinal(var5);
- } catch (BadPaddingException var9) {
- }
-
- if (var2.getParameters() != null) {
- this.encodedParams = var2.getParameters().getEncoded();
- this.paramsAlg = var2.getParameters().getAlgorithm();
- }
-
- this.sealAlg = var2.getAlgorithm();
- }
-
- protected SealedObject(SealedObject var1) {
- this.encryptedContent = (byte[])var1.encryptedContent.clone();
- this.sealAlg = var1.sealAlg;
- this.paramsAlg = var1.paramsAlg;
- if (var1.encodedParams != null) {
- this.encodedParams = (byte[])var1.encodedParams.clone();
- } else {
- this.encodedParams = null;
- }
-
- }
-
- public final String getAlgorithm() {
- return this.sealAlg;
- }
-
- public final Object getObject(Key var1) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
- if (var1 == null) {
- throw new NullPointerException("key is null");
- } else {
- try {
- return this.unseal(var1, (String)null);
- } catch (NoSuchProviderException var3) {
- throw new NoSuchAlgorithmException("algorithm not found");
- } catch (IllegalBlockSizeException var4) {
- throw new InvalidKeyException(((Throwable)var4).getMessage());
- } catch (BadPaddingException var5) {
- throw new InvalidKeyException(((Throwable)var5).getMessage());
- }
- }
- }
-
- public final Object getObject(Cipher var1) throws IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException {
- byte[] var2 = var1.doFinal(this.encryptedContent);
- ByteArrayInputStream var3 = new ByteArrayInputStream(var2);
- SunJCE_i var4 = new SunJCE_i(var3);
-
- Object var6;
- try {
- Object var5 = var4.readObject();
- var6 = var5;
- } finally {
- var4.close();
- }
-
- return var6;
- }
-
- public final Object getObject(Key var1, String var2) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException {
- if (var1 == null) {
- throw new NullPointerException("key is null");
- } else if (var2 != null && var2.length() != 0) {
- try {
- return this.unseal(var1, var2);
- } catch (IllegalBlockSizeException var4) {
- throw new InvalidKeyException(((Throwable)var4).getMessage());
- } catch (BadPaddingException var5) {
- throw new InvalidKeyException(((Throwable)var5).getMessage());
- }
- } else {
- throw new IllegalArgumentException("missing provider");
- }
- }
-
- private Object unseal(Key var1, String var2) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
- AlgorithmParameters var3 = null;
- if (this.encodedParams != null) {
- try {
- if (var2 != null) {
- var3 = AlgorithmParameters.getInstance(this.paramsAlg, var2);
- } else {
- var3 = AlgorithmParameters.getInstance(this.paramsAlg);
- }
- } catch (NoSuchProviderException var20) {
- if (var2 == null) {
- throw new NoSuchAlgorithmException(this.paramsAlg + " not found");
- }
-
- throw new NoSuchProviderException(var20.getMessage());
- }
-
- var3.init(this.encodedParams);
- }
-
- Cipher var4;
- try {
- if (var2 != null) {
- var4 = Cipher.getInstance(this.sealAlg, var2);
- } else {
- var4 = Cipher.getInstance(this.sealAlg);
- }
- } catch (NoSuchPaddingException var18) {
- throw new NoSuchAlgorithmException("Padding that was used in sealing operation not available");
- } catch (NoSuchProviderException var19) {
- if (var2 == null) {
- throw new NoSuchAlgorithmException(this.sealAlg + " not found");
- }
-
- throw new NoSuchProviderException(var19.getMessage());
- }
-
- try {
- if (var3 != null) {
- var4.init(2, var1, var3);
- } else {
- var4.init(2, var1);
- }
- } catch (InvalidAlgorithmParameterException var17) {
- throw new RuntimeException(var17.getMessage());
- }
-
- byte[] var5 = var4.doFinal(this.encryptedContent);
- ByteArrayInputStream var6 = new ByteArrayInputStream(var5);
- SunJCE_i var7 = new SunJCE_i(var6);
-
- Object var9;
- try {
- Object var8 = var7.readObject();
- var9 = var8;
- } finally {
- var7.close();
- }
-
- return var9;
- }
-
- private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
- var1.defaultReadObject();
- if (this.encryptedContent != null) {
- this.encryptedContent = (byte[])this.encryptedContent.clone();
- }
-
- if (this.encodedParams != null) {
- this.encodedParams = (byte[])this.encodedParams.clone();
- }
-
- }
- }
-